package com.qiezi.project.activity;


import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.android.exoplayer2.source.MediaSource;
import com.qiezi.R;
import com.qiezi.base.BaseMvpActivity;
import com.qiezi.bean.DataBean;
import com.qiezi.bean.IsBean;
import com.qiezi.bean.ResponseBean;
import com.qiezi.bean.VideoBean;
import com.qiezi.project.view.MFVideoPlay;
import com.qiezi.util.Request;
import com.qiezi.util.StatusBarUtil;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYVideoProgressListener;
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Call;
import tv.danmaku.ijk.media.exo2.ExoMediaSourceInterceptListener;
import tv.danmaku.ijk.media.exo2.ExoSourceManager;



public class PlayActivity extends BaseMvpActivity {

    public String TAG = getClass().getName();

    OrientationUtils orientationUtils;

    @BindView(R.id.video_player)
    MFVideoPlay videoPlayer;



    private long currentPosition = 0;
    private VideoBean data;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_play;
    }



    @Override
    protected void initData(Bundle saveBundle) {
        isAuth();
        initPlayer();
    }


    private void setUp(){
        Intent intent = getIntent();
        data = intent.getParcelableExtra("data");

        String source = data.getContent();

        log(source);

        boolean isCache = true;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            isCache = false;
        }

        videoPlayer.setUp(source,isCache,"");
        videoPlayer.startPlayLogic();

    }


    @Override
    public void showError(String msg) {
        onBackPressed();
        super.showError(msg);
    }

    private void isAuth(){
        Request.post(Request.Api.isAuth, null, new Request.JsonCallback<IsBean>(IsBean.class) {


            @Override
            public void onFailure(String msg) {
                showError(msg);
            }

            @Override
            public void onSueccess(ResponseBean<IsBean> data) {
                if (data.getData().isAuth()){
                    setUp();
                }else {
                    showError("免费播放时长已使用完");
                }
            }

        });
    }


    private void initPlayer(){

        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//        imageView.setImageResource(R.mipmap.xxx1);
        videoPlayer.setThumbImageView(imageView);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(this, videoPlayer);
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏

        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置返回按键功能

    }

    @Override
    protected void initListener() {
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orientationUtils.resolveByClick();
            }
        });

        videoPlayer.setGSYVideoProgressListener(new GSYVideoProgressListener() {
            @Override
            public void onProgress(int progress, int secProgress, int currentPosition, int duration) {
                if (currentPosition > PlayActivity.this.currentPosition){
                    PlayActivity.this.currentPosition = currentPosition;
                }
                log("progress:" + progress + " secProgress:" + secProgress + " currentPosition:" + currentPosition + " duration:" + duration);
            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoPlayer.onVideoResume();
    }

    private void reportLookLen(){
        if (data != null){
            Map<String,String> params = new HashMap<>();
            params.put("id",String.valueOf(data.getId()));
            long len = currentPosition/1000;
            params.put("len",String.valueOf(len));
            log(params);
            Request.post(Request.Api.reportLookLen,params,null);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        reportLookLen();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        //先返回正常状态
        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.getFullscreenButton().performClick();
            return;
        }
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        videoPlayer.setGSYVideoProgressListener(null);
        super.onBackPressed();
    }
}
